//
//  XQJSHandler.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/28.
//

#import "XQJSHandler.h"
#import <StoreKit/StoreKit.h>

@interface XQJSHandler()
@property (nonatomic, assign) long long startTime;
@end

@implementation XQJSHandler

-(instancetype)initWithViewController:(XQBaseViewController *)webVC configuration:(WKWebViewConfiguration *)configuration {
    self = [super init];
    if (self) {
        _webVC = webVC;
        _configuration = configuration;
        
        //注册JS 事件
        [configuration.userContentController addScriptMessageHandler:self name:@"SatellitesA"];
        [configuration.userContentController addScriptMessageHandler:self name:@"FocusedVersion"];
        [configuration.userContentController addScriptMessageHandler:self name:@"CalledWhich"];
        [configuration.userContentController addScriptMessageHandler:self name:@"InterferenceThe"];
        [configuration.userContentController addScriptMessageHandler:self name:@"ASmall"];
        [configuration.userContentController addScriptMessageHandler:self name:@"ServiceIn"];
        [configuration.userContentController addScriptMessageHandler:self name:@"ToTo"];
        [configuration.userContentController addScriptMessageHandler:self name:@"AutomaticThe"];
        
    }
    return self;
}

#pragma mark -  JS 调用 Native  代理
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
    
    NSLog(@"%@",message.body);
    
    if ([message.name isEqualToString:@"FocusedVersion"]) {
        id body = message.body;
        if ([body isKindOfClass:[NSArray class]]) {
            NSArray *datas = body;
            NSString *url = datas.firstObject;
            [XQRouter jumpFromController:[XQToolManager appRootViewController] url:url];
        }
    }else if ([message.name isEqualToString:@"SatellitesA"]) {
        //返回
        if (self.webVC.presentingViewController) {
            [self.webVC dismissViewControllerAnimated:YES completion:nil];
        }else{
            [self.webVC.navigationController popViewControllerAnimated:YES];
        }
    }else if ([message.name isEqualToString:@"CalledWhich"]) {
        id body = message.body;
        if ([body isKindOfClass:[NSArray class]]) {
            NSArray *datas = body;
            NSString *url = datas.firstObject;
            [XQRouter jumpFromController:[XQToolManager appRootViewController] url:url];
        }
    }else if ([message.name isEqualToString:@"InterferenceThe"]) {
        [self webBack];
        [XQRouter jumpFromController:[XQToolManager appRootViewController] url:@"jo://i.npe.so/united"];
    }else if ([message.name isEqualToString:@"ASmall"]) {
        if (@available(iOS 14.0, *)) {
            UIScene *currentScene = [UIApplication sharedApplication].connectedScenes.allObjects.firstObject;
            if ([currentScene isKindOfClass:[UIWindowScene class]]) {
                [SKStoreReviewController requestReviewInScene:(UIWindowScene *)currentScene];
            }
        }
    }else if ([message.name isEqualToString:@"ServiceIn"]) {
        id body = message.body;
        if ([body isKindOfClass:[NSArray class]]) {
            NSArray *datas = body;
            NSString *orderId = datas.firstObject;
           
            long long time = [XQTimeTool nowTimeStamp];
            [XQGoogleMarket reportRiskWithType:@"10" longitude:self.webVC.longitude latitude:self.webVC.latitude startTime:time endTime:time orderId:orderId];
        }
    }else if ([message.name isEqualToString:@"ToTo"]) {
        self.startTime = [XQTimeTool nowTimeStamp];
    }else if ([message.name isEqualToString:@"AutomaticThe"]) {
        [XQGoogleMarket reportRiskWithType:@"8" longitude:self.webVC.longitude latitude:self.webVC.latitude startTime:self.startTime endTime:[XQTimeTool nowTimeStamp] orderId:@""];
    }
}

-(void)webBack{
    //返回
    if (self.webVC.presentingViewController) {
        [self.webVC dismissViewControllerAnimated:YES completion:nil];
    }else{
        [self.webVC.navigationController popViewControllerAnimated:YES];
    }
    return;
}
#pragma mark -  记得要移除
-(void)cancelHandler {
    [_configuration.userContentController removeScriptMessageHandlerForName:@"backPage"];
    [_configuration.userContentController removeScriptMessageHandlerForName:@"authorization"];
    
    [_configuration.userContentController removeScriptMessageHandlerForName:@"Native"];
    [_configuration.userContentController removeScriptMessageHandlerForName:@"callApp"];
}

-(void)dealloc {
   
}


@end
